{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<bound method Tag.get_text of <table class=\"table table-striped\">\n",
      "<tr>\n",
      "<th>ACCEPT</th>\n",
      "<td>text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8</td>\n",
      "</tr>\n",
      "<tr>\n",
      "<th>ACCEPT_ENCODING</th>\n",
      "<td>gzip, deflate</td>\n",
      "</tr>\n",
      "<tr>\n",
      "<th>CONNECTION</th>\n",
      "<td>keep-alive</td>\n",
      "</tr>\n",
      "<tr>\n",
      "<th>HOST</th>\n",
      "<td>www.whatismybrowser.com</td>\n",
      "</tr>\n",
      "<tr>\n",
      "<th>USER_AGENT</th>\n",
      "<td>Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5)AppleWebKit 537.36 (KHTML, like Gecko) Chrome</td>\n",
      "</tr>\n",
      "</table>>\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "\n",
    "session = requests.Session()\n",
    "headers = {'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5)'\\\n",
    "           'AppleWebKit 537.36 (KHTML, like Gecko) Chrome',\n",
    "           'Accept':'text/html,application/xhtml+xml,application/xml;'\\\n",
    "           'q=0.9,image/webp,*/*;q=0.8'}\n",
    "url = 'https://www.whatismybrowser.com/'\\\n",
    "'developers/what-http-headers-is-my-browser-sending'\n",
    "req = session.get(url, headers=headers)\n",
    "\n",
    "bs = BeautifulSoup(req.text, 'html.parser')\n",
    "print(bs.find('table',{'class':'table-striped'}).get_text)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'domain': '.pythonscraping.com', 'expiry': 1540863803, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False, 'value': 'GA1.2.358315088.1540777403'}, {'domain': '.pythonscraping.com', 'expiry': 1603849403, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False, 'value': 'GA1.2.1302028449.1540777403'}, {'domain': '.pythonscraping.com', 'expiry': 1540777463, 'httpOnly': False, 'name': '_gat', 'path': '/', 'secure': False, 'value': '1'}, {'domain': 'pythonscraping.com', 'httpOnly': False, 'name': 'has_js', 'path': '/', 'secure': False, 'value': '1'}]\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.chrome.options import Options\n",
    "chrome_options = Options()\n",
    "chrome_options.add_argument(\"--headless\")\n",
    "driver = webdriver.Chrome(\n",
    "    executable_path='drivers/chromedriver', \n",
    "    chrome_options=chrome_options)\n",
    "driver.get('http://pythonscraping.com')\n",
    "driver.implicitly_wait(1)\n",
    "print(driver.get_cookies())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'domain': '.pythonscraping.com', 'expiry': 1540864964, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False, 'value': 'GA1.2.819982224.1540778565'}, {'domain': '.pythonscraping.com', 'expiry': 1603850564, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False, 'value': 'GA1.2.864755518.1540778565'}, {'domain': '.pythonscraping.com', 'expiry': 1540778624, 'httpOnly': False, 'name': '_gat', 'path': '/', 'secure': False, 'value': '1'}, {'domain': 'pythonscraping.com', 'httpOnly': False, 'name': 'has_js', 'path': '/', 'secure': False, 'value': '1'}]\n",
      "[{'domain': 'pythonscraping.com', 'httpOnly': False, 'name': 'has_js', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.pythonscraping.com', 'expiry': 1540778624, 'httpOnly': False, 'name': '_gat', 'path': '/', 'secure': False, 'value': '1'}, {'domain': '.pythonscraping.com', 'expiry': 1603850569, 'httpOnly': False, 'name': '_ga', 'path': '/', 'secure': False, 'value': 'GA1.2.864755518.1540778565'}, {'domain': '.pythonscraping.com', 'expiry': 1540864969, 'httpOnly': False, 'name': '_gid', 'path': '/', 'secure': False, 'value': 'GA1.2.819982224.1540778565'}]\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.chrome.options import Options\n",
    "\n",
    "chrome_options = Options()\n",
    "chrome_options.add_argument(\"--headless\")\n",
    "\n",
    "driver = webdriver.Chrome(\n",
    "    executable_path='drivers/chromedriver', \n",
    "    chrome_options=chrome_options)\n",
    "driver.get('http://pythonscraping.com')\n",
    "driver.implicitly_wait(1)\n",
    "\n",
    "savedCookies = driver.get_cookies()\n",
    "print(savedCookies)\n",
    "\n",
    "driver2 = webdriver.Chrome(\n",
    "    executable_path='drivers/chromedriver',\n",
    "    chrome_options=chrome_options)\n",
    "\n",
    "driver2.get('http://pythonscraping.com')\n",
    "driver2.delete_all_cookies()\n",
    "for cookie in savedCookies:\n",
    "    driver2.add_cookie(cookie)\n",
    "\n",
    "driver2.get('http://pythonscraping.com')\n",
    "driver.implicitly_wait(1)\n",
    "print(driver2.get_cookies())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The link http://pythonscraping.com/dontgohere is a trap\n",
      "Do not change value of phone\n",
      "Do not change value of email\n"
     ]
    }
   ],
   "source": [
    "from selenium import webdriver\n",
    "from selenium.webdriver.remote.webelement import WebElement\n",
    "from selenium.webdriver.chrome.options import Options\n",
    "\n",
    "driver = webdriver.Chrome(\n",
    "    executable_path='drivers/chromedriver',\n",
    "    chrome_options=chrome_options)\n",
    "driver.get('http://pythonscraping.com/pages/itsatrap.html')\n",
    "links = driver.find_elements_by_tag_name('a')\n",
    "for link in links:\n",
    "    if not link.is_displayed():\n",
    "        print('The link {} is a trap'.format(link.get_attribute('href')))\n",
    "\n",
    "fields = driver.find_elements_by_tag_name('input')\n",
    "for field in fields:\n",
    "    if not field.is_displayed():\n",
    "        print('Do not change value of {}'.format(field.get_attribute('name')))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
